package com.mobile.collect.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobile.collect.R;
import com.mobile.collect.beans.DevicePicInfo;
import com.mobile.collect.beans.DevicePointInfo;
import com.mobile.collect.db.EntityManager;
import com.mobile.collect.present.AddUnitPointPresent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.base.ListViewChildClickListener;
import cn.droidlover.xdroidmvp.mvp.XActivity;

public class AddUnitPointActivity extends XActivity<AddUnitPointPresent> implements ListViewChildClickListener {

    boolean isUpdate;
    Long picDbId;
    Long pointDbId;
    @BindView(R.id.contentLayout)
    LinearLayout contentLayout;

    DevicePicInfo picInfo;

    @Override
    public int getContentViewId() {
        return R.layout.activity_addunitpoint;
    }

    @Override
    public void initView() {
        super.initView();
        if (isUpdate) {
            titleTv.setText("修改点");
        } else {
            titleTv.setText("新增点");
        }
    }

    DevicePointInfo targetInfo;

    private void setDefault() {
        if (picDbId == -1) {
            return;
        }
        targetInfo = EntityManager.getInstance().queryDevicePointInfoIById(pointDbId);
        View groupView = getP().getChildGroupView(isUpdate, targetInfo);
        contentLayout.addView(groupView);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        picInfo = EntityManager.getInstance().queryDevicePicIById(picDbId);
        getP().init(isUpdate,picInfo.getParentId(), picDbId, pointDbId);
        setDefault();
    }

    @Override
    public void onRightClick(View view) {
        super.onRightClick(view);
        getP().startSave();
    }

    @Override
    public void getDataFromServer() {
    }


    @Override
    public void getIntentData() {
        picDbId = getIntent().getLongExtra("picdbid", -1);
        pointDbId = getIntent().getLongExtra("pointdbid", -1);
        isUpdate = getIntent().getBooleanExtra("isupdate", false);
    }

    @Override
    public AddUnitPointPresent newP() {
        return new AddUnitPointPresent();
    }


    public static void lunch(Activity context, boolean isUpdate, Long picDbId, Long pointDbId) {
        Intent intent = new Intent(context, AddUnitPointActivity.class);
        intent.putExtra("picdbid", picDbId);
        intent.putExtra("isupdate", isUpdate);
        intent.putExtra("pointdbid", pointDbId);
        context.startActivityForResult(intent, 0x001);
    }


    @OnClick({R.id.rightIv, R.id.addPointBtn})
    public void onViewClicked(View view) {
        List<String> list = new ArrayList<>();
        TextView valueTv = null;
        switch (view.getId()) {
            case R.id.rightIv:
                Long deviceDbId = picInfo.getParentId();
                CopyPicListActivity.lunch(this, deviceDbId, picDbId, true);
                return;
            case R.id.addPointBtn:
                addNewView();
                break;
        }
    }


    @Override
    public int getTitleLayoutResId() {
        return 0;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public boolean isNotWorkConnect() {
        return false;
    }


    private void addNewView() {
        View groupView;
        if (contentLayout.getChildCount() == 0) {
            groupView = getP().getChildGroupView(false, targetInfo);
        } else {
            groupView = getP().getChildGroupView(false, null);
        }
        contentLayout.addView(groupView);
    }


    @Override
    public void itemChildClick(int position, int viewId) {
        switch (viewId) {
            case R.id.delIv:
//                if (dataList.size() == 1) {
//                    TipDialog.show(this, "至少添加一个密封点", TipDialog.TYPE.WARNING);
//                    break;
//                }
//                SealPointInfo pointInfo = dataList.get(position);
//                if (pointInfo.getPoint_ID() != null) {
//                    EntityManager.getInstance().delDevicePointInfoById(pointInfo.getPoint_ID());
//                }
//                dataList.remove(pointInfo);
//                contentLayout.removeViewAt(position);
                break;
        }
    }

    public void remove(View view) {
        contentLayout.removeView(view);
    }

    public void saveSuccess() {
//        BusProvider.getBus().post(new AddPointEvent(picInfo.getParentId(),picInfo.get_ID()));
        startActivity(new Intent(this, DevicePointsActivity.class));
    }
}
